#coding:utf-8
from requests_html import HTMLSession
import urllib
import urllib.request
def get_url_img(url):
    session = HTMLSession() #建立一个会话
    #通过session和url得到一个响应
    req = session.get(url)
    #print(req.html.text)
    #print(req.html.links)
    #print(req.html.absolute_links)
    #select 要查找的元素
    sel = 'img'
    #在req中去查找指定元素  
    #results是找到的元素的列表  attrs是html元素的属性组成的字典
    results = req.html.find(sel)
#     for i in results:
#         print(i.attrs)
    x = 0
    for i in results:
        #打开图片的路径
        imgUrl = i.attrs['src']
        if not 'https:' in imgUrl:
            if not 'http:' in imgUrl:
                imgUrl = 'https:'+imgUrl
        try:
            resp = urllib.request.urlopen(imgUrl)
        except:
            print('有问题！')
        else:
            if 'jpg' in i.attrs['src']:
                picFile = open(r'E:\\SC\\SC1802\\Workspace\\eclipse\\1771\\day11\\Img\\%s.jpg' %x,'wb')
            elif 'png' in i.attrs['src']:
                picFile = open(r'E:\\SC\\SC1802\\Workspace\\eclipse\\1771\\day11\\Img\\%s.png' %x,'wb')
            picFile.write(resp.read())
            print('正在下载...')
            picFile.close()
            x = x + 1
get_url_img('https://www.ivsky.com/tupian/dongman_t53/')